<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;


/**
 * 发送注册验证码的logo日志
 */
class RegisterVerifyLog extends BaseModel
{
    use HasFactory;

    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'change_time';


    protected $table = 'register_verify_log';


    /**
     * 添加验证码发送日志
     * @param tel 电话号码
     */
    public function addVerifyLog($tel)
    {
        //发送成功之后，先删除之前此电话号码的验证码
        $res = $this->where('phone', $tel)->where('date', date('Y-m-d'))->first();
        if ($res) {
            $res->number = $res->number + 1;
            return $res->save();
        } else {
            $this->phone = $tel;
            $this->number = 1;
            $this->date = date('Y-m-d');
            return $this->save();
        }
    }
    /**
     * 验证发送次数
     * @param tel 电话号码
     * @param max_number 允许发送的最大次数
     */
    public function verifySendNumber($tel, $max_number = 10)
    {
        $res = $this->where('phone', $tel)->where('date', date('Y-m-d'))->first();
        if (empty($res)) {
            return true;
        }
        if ($res->number >= $max_number) {
            return '今日短信发送次数已达上限，请明日再来';
        }
        if ($res->change_time >= date('Y-m-d H:i:s', strtotime("-1 min"))) {
            return '请勿重复获取验证码';
        }
        return true;
    }
}
